package com.demon.androidbasic.dialog

import android.content.Context
import android.view.View
import android.widget.TextView
import androidx.annotation.StringRes
import com.demon.androidbasic.R

/**
 * author : Android 轮子哥
 * github : https://github.com/getActivity/AndroidProject
 * time   : 2018/12/2
 * desc   : 消息对话框
 */
class MessageDialog {
    class Builder(context: Context) : CommonDialog.Builder<Builder>(context) {
        private var mListener: OnListener? = null
        private val mMessageView: TextView
        fun setMessage(@StringRes id: Int): Builder {
            return setMessage(getString(id))
        }

        fun setMessage(text: CharSequence?): Builder {
            mMessageView.text = text
            return this
        }

        fun setListener(listener: OnListener?): Builder {
            mListener = listener
            return this
        }

        override fun create(): BaseDialog {
            // 如果内容为空就抛出异常
            require("" != mMessageView.text.toString()) { "Dialog message not null" }
            return super.create()
        }

        override fun onClick(v: View) {
            val viewId = v.id
            if (viewId == R.id.tv_ui_confirm) {
                autoDismiss()
                if (mListener != null) {
                    mListener!!.onConfirm(dialog)
                }
            } else if (viewId == R.id.tv_ui_cancel) {
                autoDismiss()
                if (mListener != null) {
                    mListener!!.onCancel(dialog)
                }
            }
        }

        init {
            setCustomView(R.layout.message_dialog)
            mMessageView = findViewById(R.id.tv_message_message)
        }
    }

    interface OnListener {
        /**
         * 点击确定时回调
         */
        fun onConfirm(dialog: BaseDialog?)

        /**
         * 点击取消时回调
         */
        fun onCancel(dialog: BaseDialog?) {}
    }
}